import { buffer } from "stream/consumers";
// import { symlinkCommon } from "./Utils";
import { WebSocketServer } from "ws";
import { ApiMsgEnum, IApiGameStartReq, IApiGameStartRes, IApiPlayerJoinReq, 
    IApiPlayerJoinRes, IApiPlayerListReq, IApiPlayerListRes, IApiRoomCreateReq, 
    IApiRoomCreateRes, IApiRoomJoinReq, IApiRoomJoinRes, IApiRoomLeaveReq, IApiRoomLeaveRes,
     IApiRoomListReq, IApiRoomListRes } from "./Common";
import { MyServer } from "./Core/MyServer";
import { Connection } from "./Core";
import { PlayerManager } from "./Biz/PlayerManager";
import { RoomManager } from "./Biz/RoomManager";
import { Room } from "./Biz/Room";



// symlinkCommon();

//在core/connection脚本添加属性playerId
declare module "./Core"{
    interface Connection{
        playerId:number;
    }
}

const server=new MyServer({
    port:9876,
});
//检测连接
server.on(("connection"),(connection:Connection)=>{
    console.log("有连接");
    console.log("来人了", server.connections.size)
});
//检测离开
server.on("disconnection",(connection:Connection)=>{
    console.log("有人走了", server.connections.size)
    if(connection.playerId){
        PlayerManager.Instance.removePlayer(connection.playerId); 
        console.log("有人走了的id是", connection.playerId);
        console.log("玩家人数是"+PlayerManager.Instance.players.size)
    }
    PlayerManager.Instance.syncPlayers();// 同步玩家列表
})



server.serApi(ApiMsgEnum.ApiPlayerJoin,(connection:Connection,data:IApiPlayerJoinReq):IApiPlayerJoinRes=>{
    const{nickname}=data;
    const player=PlayerManager.Instance.createPlayer({nickname,connection});
    connection.playerId=player.id;
    PlayerManager.Instance.syncPlayers();// 同步玩家列表
    return{
        player:PlayerManager.Instance.getPlayerView(player),
    }
});

// 玩家列表
server.serApi(ApiMsgEnum.ApiPlayerList,(connection:Connection,data:IApiPlayerListReq):IApiPlayerListRes=>{
    return{
        list:PlayerManager.Instance.getPlayersView(),
    }
});

// 房间列表
server.serApi(ApiMsgEnum.ApiRoomList,(connection:Connection,data:IApiRoomListReq):IApiRoomListRes=>{
    return{
        list:RoomManager.Instance.getRoomsView(),
    }
});
// 加入房间
server.serApi(ApiMsgEnum.ApiRoomJoin,(connection:Connection,{rid}:IApiRoomJoinReq):IApiRoomJoinRes=>{
    if(connection.playerId){
        const room=RoomManager.Instance.joinRoom(rid,connection.playerId);
        if(room){
            RoomManager.Instance.syncRooms();//  同步房间列表
            PlayerManager.Instance.syncPlayers();// 同步玩家列表
            RoomManager.Instance.syncRoom(room.id);
            return{
                room:RoomManager.Instance.getRoomView(room),
            }
        }
        else{
            throw new Error("房间不存在");
        }
    }
    else {
        throw new Error("未登录");
    }
    return 
});

server.serApi(ApiMsgEnum.ApiRoomCreate,(connection:Connection,data:IApiRoomCreateReq):IApiRoomCreateRes=>{
    if(connection.playerId){
        const newRoom=RoomManager.Instance.createRoom();
        const room=RoomManager.Instance.joinRoom(newRoom.id,connection.playerId);

        if(room){
            RoomManager.Instance.syncRooms();//  同步房间列表
            PlayerManager.Instance.syncPlayers();// 同步玩家列表
            return{
                room:RoomManager.Instance.getRoomView(room),
            }
        }
        else{
            throw new Error("房间不存在");
        }
    }
    else {
        throw new Error("未登录");
    }
    return 
});

// 离开房间
server.serApi(ApiMsgEnum.ApiRoomLeave,(connection:Connection,data:IApiRoomLeaveReq):IApiRoomLeaveRes=>{
    if(connection.playerId){
        const player=PlayerManager.Instance.idMapPlayer.get(connection.playerId)
        if(player){
            const rid=player.rid
            if(rid){
                RoomManager.Instance.leaveRoom(rid,player.id);
                RoomManager.Instance.syncRooms();//  同步房间列表
                PlayerManager.Instance.syncPlayers();// 同步玩家列表
                RoomManager.Instance.syncRoom(rid);
                return{ }
            }
            else{
                throw new Error("玩家不在房间");
            }
        }
        else{
            throw new Error("玩家不存在");
        }
    }
    else {
        throw new Error("未登录");
    }
    return 
});

//  开始游戏
server.serApi(ApiMsgEnum.ApiGameStart,(connection:Connection,data:IApiGameStartReq):IApiGameStartRes=>{
    if(connection.playerId){
        const player=PlayerManager.Instance.idMapPlayer.get(connection.playerId)
        if(player){
            const rid=player.rid
            if(rid){
                RoomManager.Instance.startRoom(rid);
                RoomManager.Instance.syncRooms();//  同步房间列表
                PlayerManager.Instance.syncPlayers();// 同步玩家列表
                RoomManager.Instance.syncRoom(rid);
                return{ }
            }
            else{
                throw new Error("玩家不在房间");
            }
        }
        else{
            throw new Error("玩家不存在");
        }
    }
    else {
        throw new Error("未登录");
    }
    return 
});


server.start().then(()=>{
    console.log("服务器启动")
}).catch(error=>{
    console.log(error);
});
